package com.todod.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.todod.common.util.SnowflakeIdGenerator;
import com.todod.mapper.RoleAuthorityMapper;
import com.todod.model.RoleAuthority;
import com.todod.service.IRoleAuthorityService;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author gsh
 * @since 2020-11-24
 */
@Service
@Transactional
public class RoleAuthorityServiceImpl extends ServiceImpl<RoleAuthorityMapper, RoleAuthority>
		implements IRoleAuthorityService {

	@Override
	public void saveData(Long _userId, Long roleId, List<RoleAuthority> roleAuthoritys) {
		deleteByRoleId(roleId);

		for (RoleAuthority roleAuthority : roleAuthoritys) {
			roleAuthority.setId(SnowflakeIdGenerator.getId());
			roleAuthority.setRoleId(roleId);
			roleAuthority.setCreateTime(LocalDateTime.now());
			roleAuthority.setCreateUserId(_userId);
			save(roleAuthority);
		}
	}

	@Override
	public void deleteByRoleId(Long roleId) {
		QueryWrapper<RoleAuthority> wrapper = new QueryWrapper<RoleAuthority>();
		wrapper.eq("ra_role_id", roleId);

		remove(wrapper);
	}

	@Override
	public void deleteByAuthorityId(Long authorityId) {
		QueryWrapper<RoleAuthority> wrapper = new QueryWrapper<RoleAuthority>();
		wrapper.eq("ra_authority_id", authorityId);

		remove(wrapper);
	}

	@Override
	public List<RoleAuthority> getRoleAuthority(Long roleId) {
		QueryWrapper<RoleAuthority> wrapper = new QueryWrapper<RoleAuthority>();
		wrapper.eq("ra_role_id", roleId);

		return list(wrapper);
	}

}
